﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinPad
{
    public partial class NumberBox : TextBox
    {
        public bool Decimal { get; set; }

        public NumberBox()
        {
            InitializeComponent();

            base.KeyPress += NumberBox_KeyPress;
        }

        public float GetFloatNumber()
        {
            if (!string.IsNullOrEmpty(base.Text))
                return Convert.ToSingle(base.Text);
            return 0;
        }

        public int GetIntNumber()
        {
            if (!string.IsNullOrEmpty(base.Text))
                return Convert.ToInt32(base.Text);
            return 0;
        }

        bool isComma(char KeyChar)
        {
            if ((KeyChar == '.' || KeyChar == ',') && Decimal == true)
                return true;
            return false;
        }

        void NumberBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !isComma(e.KeyChar);
        }
    }
}
